package com.gaoke.simpleerp.service.impl;

import com.gaoke.simpleerp.entity.sys.User;
import com.gaoke.simpleerp.exception.UserException;
import com.gaoke.simpleerp.orm.hibernate.HibernateDao;
import com.gaoke.simpleerp.service.UserService;
import com.gaoke.simpleerp.utils.MD5;

/**
 * UserService接口实现类.
 * 
 * @author weijun.huwj
 * 
 */
public class UserServiceImpl implements UserService {

    private HibernateDao<User, Long> hibernateDao;

    public void setHibernateDao(HibernateDao hibernateDao) {
        this.hibernateDao = hibernateDao;
    }

    /**
     * 
     * @see UserService#login(User) 
     */
    public void login(User user) {
        User dbUser = hibernateDao.findUniqueBy(User.PROPERTY_NAME, user
                .getName());
        if (dbUser == null) {
            throw new UserException(UserException.ERROR_CODE_USER_NOT_EXIST, "User does not exists");
        }
        if (!new MD5(user.getPassword()).compute().equalsIgnoreCase(
                dbUser.getPassword())) {
            throw new UserException(UserException.ERROR_CODE_PASSWORD_ERROR, "User password is error");
        }

    }

}
